package com.zkingsoft.wechat;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.tools.UUIDUtil;
import com.matrix.core.web.BaseAction;
import com.zkingsoft.bean.BizCoupon;
import com.zkingsoft.bean.BizMyCoupon;
import com.zkingsoft.bean.BizUser;
import com.zkingsoft.dao.BizCouponDao;
import com.zkingsoft.dao.BizMyCouponDao;
import com.zkingsoft.redis.RedisUserLoginUtils;
import com.zkingsoft.service.BizActivitiesService;
import com.zkingsoft.service.BizCollageService;

/**
 * @description (拼团)
 * @author 何明
 * @date 2018-05-23 00:08
 */
@Controller
@RequestMapping(value = "wxapi/coupon")
public class WxCouponAction extends BaseAction{

	@Autowired
	private BizActivitiesService bizActivitiesService;
	@Autowired
	private BizCollageService bizCollageService;
	@Autowired
	RedisUserLoginUtils redisUserLoginUtils;
	@Autowired
	private BizMyCouponDao bizMyCouponDao;
	@Autowired
	private BizCouponDao bizCouponDao;
	/**
	 * 用户查询我的优惠券
	 * 如果activitiesId不为空。则为查询某活动的优惠券
	 */   
   	@RequestMapping(value =  "/queryMyCoupon")
	public @ResponseBody AjaxResult queryMyCoupon(@RequestBody BizCoupon bizCoupon) {
		BizUser loginUser = redisUserLoginUtils.getLoginUser();
		bizCoupon.setUserId(loginUser.getOpenId());
		List<BizMyCoupon> list=
		bizMyCouponDao.selectByUserId(bizCoupon);
	 	return new AjaxResult(AjaxResult.STATUS_SUCCESS, list);
	 	
	}
	
   	/**
	 * 商户查询所有优惠券
	 * 状态(couStatus)： 1 已上架  2 下架 4 已过期
	 */   
   	@RequestMapping(value =  "/queryAllCoupon")
	public @ResponseBody AjaxResult queryAllCoupon(@RequestBody BizCoupon bizCoupon) {
		BizUser loginUser = redisUserLoginUtils.getLoginUser();
		bizCoupon.setUserId(loginUser.getOpenId());
	 	return new AjaxResult(AjaxResult.STATUS_SUCCESS, bizCouponDao.selectAllCoupon(bizCoupon),bizCouponDao.selectAllCouponRecord(bizCoupon));
	 	
	}

  	/**
	 * 更新优惠券状态
	 * 状态(couStatus)：1、上架  2、下架  3  删除
	 */   
   	@RequestMapping(value =  "/updateCoupon")
	public @ResponseBody AjaxResult updateCoupon(@RequestBody BizCoupon bizCoupon) {
		BizUser loginUser = redisUserLoginUtils.getLoginUser();
		if(bizCoupon==null || bizCoupon.getCouStatus()==null  || bizCoupon.getCouId()==null ){
			return new AjaxResult(AjaxResult.STATUS_FAIL, "参数错误");
		}
		BizCoupon coupon=new BizCoupon();
		if(bizCoupon.getCouStatus()==1 || bizCoupon.getCouStatus()==2 || bizCoupon.getCouStatus()==3){
			coupon.setCouStatus(bizCoupon.getCouStatus());
		}else{
			return new AjaxResult(AjaxResult.STATUS_FAIL, "参数错误");
		}
		coupon.setUserId(loginUser.getOpenId());
		coupon.setCouId(bizCoupon.getCouId());
		int i=bizCouponDao.updateByModel(coupon);
		if(i>0){
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, "操作成功");
		}
		return new AjaxResult(AjaxResult.STATUS_FAIL, "操作失败");
	 	
	}
	/**
	 * 商户新增优惠券
	 */   
   	@RequestMapping(value =  "/addCoupon")
	public @ResponseBody AjaxResult addCoupon(@RequestBody BizCoupon bizCoupon) {
		BizUser loginUser = redisUserLoginUtils.getLoginUser();
		bizCoupon.setCouId(UUIDUtil.getRandomID());
		bizCoupon.setUserId(loginUser.getOpenId());
		bizCoupon.setCreateBy(loginUser.getOpenId());
		bizCoupon.setUpdateBy(loginUser.getOpenId());
		bizCoupon.setCouSurplus(bizCoupon.getCouNum());
		int i=bizCouponDao.insert(bizCoupon);
		if(i>0){
			return new AjaxResult(AjaxResult.STATUS_SUCCESS,"成功");
		}
		return new AjaxResult(AjaxResult.STATUS_SUCCESS,"失败");
	 	
	}
}